﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using QLNhaThuoc.DTO;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc.DAL
{
    class ThuocDA : DataProvider, IMaObserver
    {
        public ThuocDA()
        {
            connect();
            fillMyTable("THUOC");
        }

        public void insert(Thuoc info)
        {
            DataRow row = myTable.NewRow();
            row["MaThuoc"] = info.MaThuoc;
            row["Ten"] = info.Ten;
            row["MaLoai"] = info.MaLoai;
            row["DonGia"] = info.DonGia;
            row["DonVi"] = info.DonVi;
            row["MaCongTy"] = info.MaCongTy;
            row["NgaySanXuat"] = info.NgaySanXuat;
            row["HanSuDung"] = info.HanSuDung;
            row["GhiChu"] = info.GhiChu;
            row["SoLuong"] = 0;
            row["TheoChiDinh"] = info.TheoChiDinh;
            myTable.Rows.Add(row);

            updateMyTable();
        }

        public void delete(Thuoc info)
        {
            myTable.Rows.Find(info.MaThuoc).Delete();

            updateMyTable();
        }

        public void updateThuoc(string attribute, string old_value, string new_value)
        {
            foreach (DataRow row in myTable.Rows)
            {
                if(row[attribute].ToString().Trim().CompareTo(old_value) == 0)
                {
                    row[attribute] = new_value;
                }
            }

            updateMyTable();
        }

        public void update(Thuoc info)
        {
            DataRow row = myTable.Rows.Find(info.MaThuoc);
            row["MaThuoc"] = info.MaThuoc.Trim();
            row["Ten"] = info.Ten;
            row["MaLoai"] = info.MaLoai;
            row["DonGia"] = info.DonGia;
            row["DonVi"] = info.DonVi;
            row["MaCongTy"] = info.MaCongTy;
            row["NgaySanXuat"] = info.NgaySanXuat;
            row["HanSuDung"] = info.HanSuDung;
            row["GhiChu"] = info.GhiChu;
            row["SoLuong"] = info.State.SoLuong;
            row["TheoChiDinh"] = info.TheoChiDinh;

            updateMyTable();
        }

        public bool KiemTraPK(Thuoc info)
        {
            DataRow row = myTable.Rows.Find(info.MaThuoc);
            if (row != null)
                return true;
            return false;
        }

        public Thuoc[] Search(string expr)
        {
            string sqlString = "select * from THUOC where " + expr;
            ArrayList list = DongHopDuLieuTuCustomDatatable(sqlString);
            int n = list.Count;
            Thuoc[] kq = new Thuoc[n];
            
            for (int i = 0; i < n; i++)
            {
                kq[i] = (Thuoc)list[i];
            }

            return kq;
        }

        public Thuoc LayThuocTheoMa(string MaThuoc)
        {
            string sqlString = "select * from THUOC where MaThuoc='" + MaThuoc + "'";
            ArrayList list = DongHopDuLieuTuCustomDatatable(sqlString);

            if(list.Count != 0)
                return (Thuoc)list[0];
            return null;
        }


        public Thuoc[] LayListThuoc()
        {
            ArrayList list = DongHopDuLieuTuDataTable();
            int n = list.Count;
            Thuoc[] kq = new Thuoc[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (Thuoc)list[i];
            }

            return kq;
        }

        public override object ConvertOneRowIntoDTO(DataTable myTable ,int i)
        {
            Thuoc obj = new Thuoc();
            obj.MaThuoc = myTable.Rows[i]["MaThuoc"].ToString();
            obj.Ten = myTable.Rows[i]["Ten"].ToString();
            obj.MaLoai = myTable.Rows[i]["MaLoai"].ToString();
            obj.DonGia = int.Parse(myTable.Rows[i]["DonGia"].ToString());
            obj.DonVi = myTable.Rows[i]["DonVi"].ToString();
            obj.MaCongTy = myTable.Rows[i]["MaCongTy"].ToString();
            obj.NgaySanXuat = DateTime.Parse(myTable.Rows[i]["NgaySanXuat"].ToString());
            obj.HanSuDung = DateTime.Parse(myTable.Rows[i]["HanSuDung"].ToString());
            obj.GhiChu = myTable.Rows[i]["GhiChu"].ToString();
            obj.State.SoLuong = int.Parse(myTable.Rows[i]["SoLuong"].ToString());
            obj.TheoChiDinh = bool.Parse(myTable.Rows[i]["TheoChiDinh"].ToString());

            return obj;
        }

        public void UpdateMaCongTy(object old_info, object info)
        {
            updateThuoc("MaCongTy", ((CongTy)old_info).MaCT, ((CongTy)info).MaCT);
        }
    }
}
